package com.eric.downloader;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class BDSDownloader extends SimplePageDownloader{
	
	String queryBaseUrl = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=";
	
	String kw;

	public static void main(String[] args) {
		BDSDownloader bd = new BDSDownloader("美妆");
		System.out.println(bd.analyze());
	}
	
	public BDSDownloader () {
		
	}
	
	public BDSDownloader (String kw) {
		this.kw = kw;
	}
	
	public BDSDownloader (String kw,MyProxy p) {
		super(p);
		this.kw = kw;
	}
	
	public JSONObject analyze () {
		return parseBDS(querySearchEngine(queryBaseUrl,kw));
	}
	
	public JSONObject analyze (String kw) {
		this.kw = kw;
		return analyze();
	}	

	JSONObject parseBDS (String bds) {
		JSONObject json = new JSONObject();
		int p1=0,p2 = 0;
		
		//Parse s String
		List<String> list = new ArrayList<String>();
		p1=0;
		p2=0;
		String s;
		s = bds.substring(bds.indexOf("[")+1,bds.indexOf("]"));
		for (int i=0;i<s.length();i++) {
			if (String.valueOf(s.charAt(i)).equals("\"")) {
				if(p1 < p2 || p1 == 0) {
					p1 = i + 1;
				}else{
					p2 = i;
					list.add(s.substring(p1,p2));
				}
			}
		}
		json.put("kw",kw);
		json.put("rec", list);
		return json;
	}
}
